package com.example.zhinongbao.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.zhinongbao.R;
import com.example.zhinongbao.utils.Constant;
import com.example.zhinongbao.utils.FragmentUtils;

/**
 * 我的页面
 */
public class MineFragment extends Fragment {
    // 用户账号
    private TextView text_count;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        initView();
    }

    private void initView() {
        text_count = getView().findViewById(R.id.text_count);
        // 使用全局变量的账号
        if (!Constant.userAccount.isEmpty()) {
            text_count.setText(Constant.userAccount);
        }
        getView().findViewById(R.id.ll_settings).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 切换到设置页面
                FragmentUtils.getInstance().switchFragment(R.id.nav_setting);
            }
        });
        // 如果是处于登录状态，点击头像弹窗提示退出登录，如果处于未登录状态，点击头像登录
        getView().findViewById(R.id.image_avator).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Constant.isLogin) {
                    showLogoutDialog();
                } else {
                    FragmentUtils.getInstance().switchFragment(R.id.nav_login);
                }
            }
        });
        getView().findViewById(R.id.ll_car).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 切换到购物车页面
                FragmentUtils.getInstance().switchFragment(R.id.nav_car);
            }
        });
    }

    /**
     * 退出登录对话框
     */
    private void showLogoutDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("是否退出登录")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // 确定退出登录了，将全局变量的登录状态修改为false，并将用户账号名显示为请登录
                        Constant.isLogin = false;
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                text_count.setText("请登录");
                            }
                        });
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                    }
                });
        builder.create();
        builder.show();
    }

}
